package com.cn.algorithm.two.union;

public abstract class UnionFind {

    int[] parent;

    public UnionFind(int capacity) {
        parent = new int[capacity];
        for (int i = 0; i < capacity; i++) {
            parent[i] = i;
        }
    }

    abstract int find(int v);

    abstract void union(int v1, int v2);

    public boolean isSame(int v1, int v2) {
        return find(v1) == find(v2);
    }

    public void checkIndex(int index) {
        if (index < 0 || index >= parent.length) {
            throw new IndexOutOfBoundsException();
        }
    }
}
